﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace workshop3
{
    public partial class Formaddams : Form
    {
        public DataTable dtams = new DataTable();
        DataColumn dcpercentage = new DataColumn("percentage(%)");
        DataColumn itemname = new DataColumn("itemname");
        //public int iconfirm = 0;
        int percentage = 0;
        public Formaddams()
        {
            InitializeComponent();
        }

        private void Formaddams_Load(object sender, EventArgs e)
        {
            dtams.Columns.Add(itemname);
            dtams.Columns.Add(dcpercentage);
        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            //check if the input is legal or not
            if (!ClassCreateXML.TextCheck(txtitemname.Text))
            {
                return;
            }
            if (ClassCreateXML.TextCheck(txtpercentage.Text))
            {
                return;
            }
            int temp = 0;
            int.TryParse(txtpercentage.Text, out temp);
            percentage = percentage + temp;
            if (percentage <= 100)
            {
                dtams.Rows.Add(txtitemname.Text, txtpercentage.Text );
                dgvams.DataSource = dtams;
            }
            else
            {
                MessageBox.Show("The total percentage is overhead 100. Please input valid percentage!");
                percentage = percentage - temp;
            }
            txtitemname.Text = string.Empty;
            txtpercentage.Text = string.Empty;
            lbltotal.Text = percentage.ToString();
        }

        private void btnconfirm_Click(object sender, EventArgs e)
        {
            if (percentage != 100)
            {
                return;
            }
            if (dtams.Rows.Count > 0)//check if the user has been add something
            {
                DialogResult dr = MessageBox.Show("Once you have been pressed the ‘Confirm' button, you do not allow to change!", "Warning", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.OK)
                {
                    Formcoursecreate.iconfirm = 1;
                    this.Close();
                }
                if (dr == DialogResult.Cancel)
                {

                }
            }
            else {
                MessageBox.Show("You have not input any data.");
            }
           
        }

        private void dgvams_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)//rigth click do the delete
        {
            if (e.Button == MouseButtons.Right)
            {
                percentage = 0;
                int temp;
                //int.TryParse(dtams.Rows[e.RowIndex][1].ToString(), out temp);
                //percentage = percentage - temp;
                dtams.Rows.Remove(dtams.Rows[e.RowIndex]);
                for (int i = 0; i < dtams.Rows.Count; i++)
                {
                    int.TryParse(dtams.Rows[i][1].ToString(), out temp);
                    percentage = percentage + temp;
                }
                lbltotal.Text = percentage.ToString();
            }
        }

        private void dgvams_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            percentage = 0;
            int temp;
            //int.TryParse(dtams.Rows[e.RowIndex][1].ToString(), out temp);
            for (int i = 0; i < dtams.Rows.Count; i++)
            {
                int.TryParse(dtams.Rows[i][1].ToString(), out temp);
                percentage = percentage + temp;
            }
            lbltotal.Text = percentage.ToString();
        }
    }
}
